

package sample;

import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Stop;
import javafx.stage.Stage;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class Controller {



    /*===========================采集界面========================*/


    public Label submittedSuccess;
    public TextField No;
    public TextField Weather;
    public TextField collecDate;
    public TextField Humidity;
    public TextField Pollute;
    public TextField AirPollute;

    public void SubmitData() {
        //获得随机编号
        No.setText(UUID.randomUUID().toString().substring(0, 16));
        //指定范围的温度
        Weather.setText(String.valueOf(35 + (new Random().nextDouble() * 5)).substring(0, 5) + "°");
        //日期
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStr2 = sf.format(date);
        collecDate.setText(timeStr2);
        System.out.println(timeStr2);
        //湿度
        Humidity.setText(String.valueOf(50 + (new Random().nextDouble() * 5)).substring(0, 5) + "%");
        //污染程度
        ArrayList<String> pollutes = new ArrayList<>();
        pollutes.add("Ⅰ级(特大)");
        pollutes.add("Ⅱ级(重大)");
        pollutes.add("Ⅲ级(较大)");
        pollutes.add("Ⅳ级(一般)");
        int rand = new Random().nextInt(4);
        System.out.println(String.valueOf(rand));
        Pollute.setText(pollutes.get(rand));

        //污染物
        ArrayList<String> airPollutes = new ArrayList<>();
        airPollutes.add("臭氧");
        airPollutes.add("甲烷");
        airPollutes.add("丙烯醛");
        airPollutes.add("二氧化硫");
        airPollutes.add("氮氧化合物");
        airPollutes.add("一氧化碳");
        airPollutes.add("粉尘/可吸入颗粒物");
        //至少1种污染物
        int airrand = 1 + new Random().nextInt(airPollutes.size() - 1);

        //新建一个ArrayList，将要显示的污染物存在里面
        ArrayList<String> allairpullute = new ArrayList<>();
        for (int i = 0; i < airrand; i++) {
            allairpullute.add(airPollutes.get(i));
        }
//        把所有要显示的污染物放入前端要显示的方框中
        AirPollute.setText(String.valueOf(allairpullute));

        System.out.println("点击采集按钮");
        submittedSuccess.setText("采集成功！");
    }



    /*===========================查询界面========================*//*
     */
/*

    public Label searchSuccess;
    *//*

     */
/**
 * 原来的手动输入数据的折线图
 * 用循环写入数据的折线图
 *//*
     */
/*

    public void originalSearchData() {
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("监测数据");
        final LineChart<String, Number> lineChart =
                new LineChart<String, Number>(xAxis, yAxis);
        lineChart.setTitle("环境监测图表");
        XYChart.Series series1 = new XYChart.Series();
        series1.setName("温度变化");
        series1.getData().add(new XYChart.Data("Jan", 12.5));
        series1.getData().add(new XYChart.Data("Feb", 15.4));
        series1.getData().add(new XYChart.Data("Mar", 16.2));
        series1.getData().add(new XYChart.Data("Apr", 18.5));
        series1.getData().add(new XYChart.Data("May", 21.4));
        series1.getData().add(new XYChart.Data("Jun", 25.9));
        series1.getData().add(new XYChart.Data("Jul", 29.2));
        series1.getData().add(new XYChart.Data("Aug", 32.1));
        series1.getData().add(new XYChart.Data("Sep", 30.3));
        series1.getData().add(new XYChart.Data("Oct", 26.1));
        series1.getData().add(new XYChart.Data("Nov", 22.7));
        series1.getData().add(new XYChart.Data("Dec", 19.2));
        XYChart.Series series2 = new XYChart.Series();
        series2.setName("湿度变化");
        series2.getData().add(new XYChart.Data("Jan", 33));
        series2.getData().add(new XYChart.Data("Feb", 34));
        series2.getData().add(new XYChart.Data("Mar", 25));
        series2.getData().add(new XYChart.Data("Apr", 44));
        series2.getData().add(new XYChart.Data("May", 39));
        series2.getData().add(new XYChart.Data("Jun", 16));
        series2.getData().add(new XYChart.Data("Jul", 55));
        series2.getData().add(new XYChart.Data("Aug", 54));
        series2.getData().add(new XYChart.Data("Sep", 48));
        series2.getData().add(new XYChart.Data("Oct", 27));
        series2.getData().add(new XYChart.Data("Nov", 37));
        series2.getData().add(new XYChart.Data("Dec", 29));
        XYChart.Series series3 = new XYChart.Series();
        series3.setName("污染物变化");
        series3.getData().add(new XYChart.Data("Jan", 44));
        series3.getData().add(new XYChart.Data("Feb", 35));
        series3.getData().add(new XYChart.Data("Mar", 36));
        series3.getData().add(new XYChart.Data("Apr", 33));
        series3.getData().add(new XYChart.Data("May", 31));
        series3.getData().add(new XYChart.Data("Jun", 26));
        series3.getData().add(new XYChart.Data("Jul", 22));
        series3.getData().add(new XYChart.Data("Aug", 25));
        series3.getData().add(new XYChart.Data("Sep", 43));
        series3.getData().add(new XYChart.Data("Oct", 44));
        series3.getData().add(new XYChart.Data("Nov", 45));
        series3.getData().add(new XYChart.Data("Dec", 44));

        XYChart.Series series4 = new XYChart.Series();
        series4.setName("污染程度");
        series4.getData().add(new XYChart.Data("Jan", 24));
        series4.getData().add(new XYChart.Data("Feb", 15));
        series4.getData().add(new XYChart.Data("Mar", 44));
        series4.getData().add(new XYChart.Data("Apr", 54));
        series4.getData().add(new XYChart.Data("May", 13));
        series4.getData().add(new XYChart.Data("Jun", 57));
        series4.getData().add(new XYChart.Data("Jul", 24));
        series4.getData().add(new XYChart.Data("Aug", 57));
        series4.getData().add(new XYChart.Data("Sep", 21));
        series4.getData().add(new XYChart.Data("Oct", 33));
        series4.getData().add(new XYChart.Data("Nov", 23));
        series4.getData().add(new XYChart.Data("Dec", 47));


        Scene scene = new Scene(lineChart, 800, 600);
        lineChart.getData().addAll(series1, series2, series3, series4);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();
    }
    *//*

     */

    /**
     * 用循环写入数据的折线图
     */


    public void SearchData() {
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("监测数据");
        final LineChart<String, Number> lineChart =
                new LineChart<String, Number>(xAxis, yAxis);
        lineChart.setTitle("环境监测图表");
        double[] arr = new double[12];
        String[] Months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
                "Sep", "Oct", "Nov", "Dec"};


        XYChart.Series series1 = new XYChart.Series();
        series1.setName("温度变化");
        for (int i = 0; i < 6; i++) {
            arr[i] = arr[i] + (10 + new Random().nextDouble() * 3);
        }
        for (int i = 6; i < 12; i++) {
            arr[i] = (15 + new Random().nextDouble() * 3);
        }
        for (int i = 0; i < 12; i++) {
            series1.getData().add(new XYChart.Data(Months[i], Double.parseDouble(String.valueOf(arr[i]))));
        }


        XYChart.Series series2 = new XYChart.Series();
        series2.setName("湿度变化");
        for (int i = 0; i < 12; i++) {
            arr[i] = (45 + new Random().nextDouble() * 10);
        }
        for (int i = 0; i < 12; i++) {
            series2.getData().add(new XYChart.Data(Months[i], Double.parseDouble(String.valueOf(arr[i]))));
        }


        XYChart.Series series3 = new XYChart.Series();
        series3.setName("污染物变化");
        for (int i = 0; i < 12; i++) {
            arr[i] = 10 + new Random().nextDouble() * 10;
        }
        for (int i = 0; i < 12; i++) {
            series3.getData().add(new XYChart.Data(Months[i], Double.parseDouble(String.valueOf(arr[i]))));
        }


        XYChart.Series series4 = new XYChart.Series();
        series4.setName("污染程度");
        for (int i = 0; i < 12; i++) {
            arr[i] = 1 + new Random().nextDouble() * 4;
        }
        for (int i = 0; i < 12; i++) {
            series4.getData().add(new XYChart.Data(Months[i], Double.parseDouble(String.valueOf(arr[i]))));
        }

        Scene scene = new Scene(lineChart, 800, 600);
        lineChart.getData().addAll(series1, series2, series3, series4);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();
    }



/*===========================后台界面========================*/


    public Label updateSuccess;
    public void Exit() {
        System.out.println("点击退出按钮");
    }
    public void UpdateData() {
        System.out.println("点击修改按钮");
        updateSuccess.setText("修改成功！");
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("更新成功！");
        alert.showAndWait();
        System.out.println("点击删除按钮");
    }
    //后台的查询功能
    public TextField weather;
    public TextField humid;
    public TextField pollute;
    public void manageSearch() {
        //指定范围的温度
        weather.setText(String.valueOf(35 + (new Random().nextDouble() * 5)).substring(0, 5) + "°");
        //湿度
        humid.setText(String.valueOf(50 + (new Random().nextDouble() * 5)).substring(0, 5) + "%");
        //污染程度
        ArrayList<String> pollutes = new ArrayList<>();
        pollutes.add("Ⅰ级(特大)");
        pollutes.add("Ⅱ级(重大)");
        pollutes.add("Ⅲ级(较大)");
        pollutes.add("Ⅳ级(一般)");
        int rand = new Random().nextInt(4);
        System.out.println(String.valueOf(rand));
        pollute.setText(pollutes.get(rand));
    }
    //点击删除后，文本框内容清空
    public void deleteBtn() {
        Alert alert = new Alert(Alert.AlertType.WARNING);
//        alert.setTitle("删除成功！");
        alert.setHeaderText("删除成功！");
//        alert.setContentText("");
        alert.showAndWait();
        System.out.println("点击删除按钮");

        weather.setText(" ");
        humid.setText(" ");
        pollute.setText(" ");
//        nowdate.isShowing();

    }
}






